
package view;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

/**
 * @version 1.0
 * @date 10/04/2012
 * @author john
 */
public class ContactDialog extends Stage{

    private final Group root;
    private final Scene scene;
    private final GridPane grid;
    
    private final TextField subjectTF;
    private final TextArea messageTA;
    private Label subjectL;
    private Label messageL;
    
    private final Button sendB;
    private final Button cancelB;
    
    public ContactDialog(Stage owner) {
        super();
        initOwner(owner);
        initModality(Modality.APPLICATION_MODAL);
        root=new Group();
        scene=new Scene(root);
        scene.setFill(Color.WHITE);
        setScene(scene);
        
        grid=new GridPane();
        grid.setPadding(new Insets(20));
        grid.setHgap(10);
        grid.setVgap(6);
        
        subjectL=new Label("Asunto");
        subjectTF=new TextField("No puedo iniciar sesión");
        messageL=new Label("Mensaje");
        messageTA=new TextArea("Ingreso mi usuario y contraseña de siempre y no puedo iniciar sesión.");
        messageTA.setMinSize(300, 200);
        sendB=new Button("Enviar");
        sendB.setOnAction(sendAction());
        cancelB=new Button("Cancelar");
        cancelB.setOnAction(cancelAction());
        HBox buttonsHBox=new HBox(7);
        GridPane.setHalignment(buttonsHBox, HPos.RIGHT);
        GridPane.setMargin(buttonsHBox, new Insets(2, 0, 0, 0));
        buttonsHBox.getChildren().addAll(sendB,cancelB);
        
        grid.add(subjectL, 0, 0);
        grid.add(subjectTF, 0, 1);
        grid.add(messageL, 0, 2);
        grid.add(messageTA, 0, 3);
        grid.add(buttonsHBox, 0, 4);
        
        root.getChildren().add(grid);
        setTitle("Contacto con el administrador");
        setResizable(false);
        setOnCloseRequest(cosumeCloseRequest());
    }
    
    /**
     * 
     */
    public void showStage(){
        show();
        messageTA.requestFocus();
        messageTA.end();
    }
    
    /**
     * 
     * @return 
     */
    private EventHandler<WindowEvent> cosumeCloseRequest(){
        return new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent event) {
                event.consume();
            }
        };
    }
    
    /**
     * 
     * @return 
     */
    private EventHandler<ActionEvent> sendAction(){
        return new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                // Validar entradas y enviar el correo
                System.err.println("Envio no implementado.");
            }
        };
    }
    
    /**
     * 
     * @return 
     */
    private EventHandler<ActionEvent> cancelAction(){
        return new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                MainWindow.closeContactDialog();
                MainWindow.showLoginDialog();
            }
        };
    }
    
}